var userId = userid
var publicImg;
$(function(){
	$("#usersId").val(userid);
	initData();
	$("#supplyName").click(function(){
		localStorage.setItem("supplyId", $("#supplyId").val());
		localStorage.setItem("supplyName", $("#supplyName span").text());
		if(null != localStorage.getItem("lawer")){
			localStorage.removeItem("lawer");
		}
		location.href="supply.do";
	});
	$("#lawer").click(function(){
		localStorage.setItem("supplyId", $("#supplyId").val());
		localStorage.setItem("lawer", $("#lawer span").text());
		if(null != localStorage.getItem("supplyName")){
			localStorage.removeItem("supplyName");
		}
		location.href="supply.do";
	});
	$("#identyCardPathInput").click(function(){
		publicImg = $("#identyCardPath");
		openSelectImgDiv();
	});
	$("#lawPathInput").click(function(){
		publicImg = $("#lawPath");
		openSelectImgDiv();
	});
	getPic();
	$("#headPathInput").click(function(){
		publicImg = $("#picPath");
		openSelectImgDiv();
	});
	
	});
function initData(){
	$.ajax({
		type : 'POST',
		async : false,
		data : {userid:userid},
		url : server_url+'rest/supply/getSupply.do?ran='+Math.random(),
		success : function(data) {
			if (data.retCode == "0000") {
				var pasteData = data.data;
				/*$("#picPath").attr("src",pasteData.picPath);*/
				$("#supplyName").attr("value",pasteData.supplyName);
			    $("#supplyName span").html(pasteData.supplyName);
				$("#lawer").attr("value",pasteData.lawer);
				$("#lawer span").html(pasteData.lawer);
				$("#identyCardPath").attr("src",pasteData.identyCardPath);
				$("#identyCardPath").val(pasteData.identyCardPath);
				$("#lawPath").attr("src",pasteData.lawPath);
				$("#lawPath").val(pasteData.lawPath);
				$("#supplyId").val(pasteData.supplyId);
			} else{
				easyAlert("操作失败");
			}
		}
	});
}

function cardImagEdit(data,input){
	var supplyId=$("#supplyId").val();
	//var id = $(input).attr("id").substring(11);
	$.ajax({
		type:"POST",
		async : false,
		data:{identyCardPath:data,supplyId:supplyId},
		url : server_url+'rest/supply/supplyEdit.do?ran='+Math.random(),
		success:function(data){
			if(data.retCode == "0000"){
				var path = data.data;
				$("input[name=identyCardPath]").val(path);
				input.attr("src",path);
				cancelSelectImgDiv();
				/*$("#"+id).siblings("input[type=hidden]").val(path);
				$("#"+id).siblings("img").attr("src",path);*/
			}else{
			easyAlert("网络异常,请稍后再试");
			}
		},
	});
}
function lawImagEdit(data,input){
	var supplyId=$("#supplyId").val();
	//var id = $(input).attr("id").substring(11);
	$.ajax({
		type:"POST",
		async : false,
		data:{lawPath:data,supplyId:supplyId},
		url : server_url+'rest/supply/supplyEdit.do?ran='+Math.random(),
		success:function(data){
			if(data.retCode == "0000"){
				var path= data.data;
				$("input[name=lawPath]").val(path);
				input.attr("src",path);
				cancelSelectImgDiv();
				/*$("#"+id).siblings("input[type=hidden]").val(path);
				$("#"+id).siblings("img").attr("src",path);*/
			}else{
			easyAlert("网络异常,请稍后再试");
			}
		},
	});
}

function getPic(){
	$.ajax({
		type : 'POST',
		async : false,
		data : {userId:userId},
		url : server_url+'rest/frontUser/getPic.do?ran='+Math.random(),
		success : function(data) {
			if (data.retCode == "0000") {
				var pasteData = data.data;
				$("#compName").text(pasteData.buyerInfoVo.compName);
				if(pasteData.picPath != null && pasteData.picPath !=""){
				$("#picPath").attr("src",pasteData.picPath);
				$("#picPath").val(pasteData.picPath);
				}else{
					$("#picPath").attr("src","img/user.png");
				}
			} else{
				easyAlert("操作失败");
			}
		}
	});
}


function headImagEdit(data,input){
	//var id = $(input).attr("id").substring(11);
	//var input= $("#identyCardPath");
	$.ajax({
		type:"POST",
		async : false,
		data:{identyCardPath:data,userId:userId},
		url : server_url+'rest/frontUser/updatePic.do?ran='+Math.random(),
		success:function(data){
			if(data.retCode == "0000"){
				var path = data.data;
				$("input[name=picPath]").val(path);
				input.attr("src",path);
				cancelSelectImgDiv();
				//$("#"+id).siblings("input[type=hidden]").val(path);
				//$("#"+id).siblings("img").attr("src",path);
			}else{
			easyAlert("网络异常,请稍后再试");
			}
		},
	});
}

function backing(){
	window.location.href=server_url+"opration.do";
	//localStorage.setItem("opration","opration");
}
/*上传图片开始*/
//取消
function cancelSelectImgDiv(){
	
	$(".moskImg").css("display","none");
	$(".selecImg").css({
		"display":"block",
		bottom:'-2.92rem',
		animation:'0.5s move1',
	});
	setTimeout(function(){$("#selecImg").css({
		display:'none'
	})},500);
}
//打开选择窗
function openSelectImgDiv(){
	$(".moskImg").css({
		display:'block'
	});
	$("#selecImg").css({
		display:"block",
		bottom:'0rem',
		animation:'0.5s move'
	});
}
//调用摄像头
function takePhoto(){
	window.photo.openCamera();
}

//调用相册
function chooseAlbum(){
	window.photo.openCamera1();
}

//获取返回路径
function getPicture(path){
	
	var s = replaceAll(path,'//','/');
	s = replaceAll(s,'"','');
	//s就是处理过的返回路径
	//publicImg.attr('src',s);
	saveImgData(s,publicImg);
	
	
}
//获取返回路径
function getPicture1(path){
	var s = replaceAll(path,'//','/');
	s = replaceAll(s,'"','');
	//publicImg.attr('src',s);
	saveImgData(s,publicImg);
	//alert(path);
}
//字符串替换
function replaceAll(s,s1,s2){
	while(s.indexOf(s1)>=0){
		s = s.replace(s1,s2);
	}
	return s;
}

function saveImgData(path,node){
	if(node.attr('id') == 'picPath'){
		headImagEdit(path,node);
	}
	if(node.attr('id') == 'identyCardPath'){
		lawImagEdit(path,node);
	}
	if(node.attr('id') == 'lawPath'){
		
		cardImagEdit(path,node);
	}
}

